const express = require('express')
const Router = express.Router()
const model = require('./model')
const User = model.getModel('user')
let code = 1;
Router.get('/list', function(req, res) {
	User.find({}, function(err, doc) {
		return res.json(doc)
	})
})
Router.post('/register', function(req, res) {
	const {user, pwd, type} = req.body;
	User.findOne({
		user
	}, function(err, doc) {
		if (doc) {
			return res.json({code: 1, msg: '用户名重复'})
		}
		User.create({
			user,
			pwd,
			type
		}, function(err, doc) {
			if (err) {
				return res.json({code: 1, msg: '后端出错了'})
			}

			return res.json({code: 0})
		})
	})
})
Router.get('/info', function(req, res) {
	return res.json({code}) //返回登录状态
})
Router.post('/login', function(req, res) {
	const {user, pwd} = req.body;
	User.findOne({
		user
	}, function(err, doc) {
		console.log(err)
		if (doc) {
			code = 0
			return res.json({code})
		} else {
			code = 1
			return res.json({code, msg: err}) //返回登录状态
		}

	})
})
module.exports = Router
